package gui_layer;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.Border;

@SuppressWarnings("serial")
public abstract class UniversalManageWindow extends UniversalTableWindow
{
	protected JTextField search_field;
	protected JCheckBox show_deleted_chckbox;
	protected JLabel search_lbl;
	private JButton btnDelete;
	private JButton btnRestore;
	private JButton btnView;



	/**
	 * Create the frame.
	 */
	public UniversalManageWindow()
	{
		super();
		make_option_buttons();
		make_checkbox();
		make_search_field();
		set_table_params();
	}
	
	private void make_option_buttons()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JButton btnNew = new JButton("New");
		btnNew.setForeground(Color.WHITE);
		btnNew.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnNew.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnNew.setContentAreaFilled(false);
		btnNew.setOpaque(true);
		btnNew.setBorder(new RoundedCornerBorder());
		option_button_panel.add(btnNew);
		btnNew.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { create_new(); }});
		
		JButton btnEdit = new JButton("Edit");
		btnEdit.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnEdit.setForeground(Color.WHITE);
		btnEdit.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnEdit.setContentAreaFilled(false);
		btnEdit.setOpaque(true);
		btnEdit.setBorder(new RoundedCornerBorder());
		btnEdit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { edit(); }});
		option_button_panel.add(btnEdit);
		
		btnView = new JButton("View");
		btnView.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnView.setContentAreaFilled(false);
		btnView.setOpaque(true);
		btnView.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnView.setForeground(Color.WHITE);
		btnView.setBorder(new RoundedCornerBorder());
		btnView.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { view(); }});
		option_button_panel.add(btnView);
		
		btnDelete = new JButton("Delete");
		btnDelete.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnDelete.setBorder(new RoundedCornerBorder());
		btnDelete.setContentAreaFilled(false);
		btnDelete.setOpaque(true);
		btnDelete.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnDelete.setForeground(Color.WHITE);
		btnDelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { delete(); }});
		option_button_panel.add(btnDelete);
		
		btnRestore = new JButton("Restore");
		btnRestore.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnRestore.setContentAreaFilled(false);
		btnRestore.setOpaque(true);
		btnRestore.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnRestore.setForeground(Color.WHITE);
		btnRestore.setBorder(new RoundedCornerBorder());
		btnRestore.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0){ restore(); }});
		btnRestore.setVisible(false);
		option_button_panel.add(btnRestore);
	}
	
	private void make_checkbox()
	{
		show_deleted_chckbox = new JCheckBox("New check box");
		show_deleted_chckbox.setForeground(Color.WHITE);
		show_deleted_chckbox.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		show_deleted_chckbox.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		show_deleted_chckbox.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				if(show_deleted_chckbox.isSelected())
				{
					show_deleted();
					btnDelete.setVisible(false);
					btnRestore.setVisible(true);
				}
				else
				{
					show_non_deleted();
					btnDelete.setVisible(true);
					btnRestore.setVisible(false);
				}
				
			}
		});
		display_panel.add(show_deleted_chckbox);
	}
	
	private void make_search_field()
	{
		search_field = new JTextField();


		Border roundedBorder = new RoundedCornerBorder(); // the third parameter - true, says it's round
		search_field.setBorder(roundedBorder);


		search_field.addKeyListener(new KeyAdapter()
		{			
			@Override
			public void keyReleased(KeyEvent arg0)
			{
				dynamically_search();
			}
			
		});
		search_field.addFocusListener(new FocusAdapter()
		{
			public void focusGained(FocusEvent e)
			{
				if(show_deleted_chckbox.isSelected())
				{
					show_deleted();
				}
				else
				{
					show_non_deleted();
				}
			}
		});
		search_lbl = new JLabel("Name");
		search_lbl.setForeground(Color.WHITE);
		search_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		search_set_panel.add(search_lbl);
		search_set_panel.add(search_field);
		search_field.setColumns(10);
	}

	private void set_table_params()
	{
		
		table.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				if (e.getClickCount() == 2)
				{
					view();
				}
			}
		});
		
		table.addKeyListener(new KeyAdapter()
		{
			public void keyTyped(KeyEvent e)
			{
				if (e.getKeyChar() == '\u007F')
				{
					if(show_deleted_chckbox.isSelected())
					{
						restore();
					}
					else
					{
						delete();
					}
				}
			}
		});
	}

	protected abstract void dynamically_search();
	
	protected abstract void show_deleted();
	
	protected abstract void show_non_deleted();
	
	protected abstract void create_new();
	
	protected abstract void edit();
	
	protected abstract void view();
	
	protected abstract void delete();
	
	protected abstract void restore();
	
	
}
